//
//  LoadingView.m
//  LimitFree
//
//  Created by qianfeng on 15/8/27.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "LoadingView.h"
static LoadingView *loading=nil;
@implementation LoadingView
+(LoadingView*)shareSingleton{
    if (!loading) {
        //一0000的状态初始化
        //只有在展示的时候才能展示区域的大小
        loading=[[LoadingView alloc]initWithFrame:CGRectZero];
        //加背景
        loading.backgroundColor=[UIColor whiteColor];
    }
    return loading;
}
+(void)showLoadingView:(UIView*)superView{
    if (loading==nil) {
        loading=[LoadingView shareSingleton];
        
        
       
    }
    //停靠模式
    loading.autoresizesSubviews=YES;
    loading.frame = superView.frame;
    [superView addSubview:loading];
}
+(void)hideLoadingView{
    [loading hideView];
    
    [UIView animateWithDuration:0.5 animations:^{
        //loading.frame=CGRectZero;
        
        //[loading setYOffset:loading.];
        //[loading setYOffset:200];
        [loading setYOffset:loading.superview.height];
    }completion:^(BOOL finished){
        [loading removeFromSuperview];
    }];
}


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
//创建loadingView的视图内容
-(void)createView{
    //创建背景黑色视图
    [self createBackView];
    //创菊花
    [self createActivityView];
   //创建提示标签
    [self createLabel];
}
-(void)createActivityView{
    _activity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    //[_activity setXOffset:30];
    //[_activity setYOffset:30];
    _activity.frame=CGRectMake((_backView.width-_activity.width)/2, (_backView.height-_activity.height)/2-LeftDistance, _activity.width, _activity.height);
  
    //当菊花停止时不隐藏
    _activity.hidesWhenStopped=NO;
    [_activity startAnimating];
    [_backView addSubview:_activity];
}
-(void)createLabel{
    _alertLabel=[Factory createLabelWithTitle:@"玩命加载中" frame:CGRectMake(0,_activity.bottom+ControlDistance, _backView.width, _backView.height-_activity.bottom-ControlDistance)];
    [_alertLabel setYOffset:78];
    //设置
    _alertLabel.textAlignment=NSTextAlignmentCenter;
    _alertLabel.textColor=[UIColor whiteColor];
    [_backView addSubview:_alertLabel];
}
-(void)createBackView{
    _backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    
    _backView.center=self.center;
    
    //加圆角
    _backView.layer.cornerRadius=6.f;
    _backView.layer.masksToBounds=YES;
    /*
     1、创建两个View*对象：View1      View2
     
     2、view1.autoresizesSubviews =YES; 是个BOOL类型  是否开启自适应。
     如果写NO的话，那么view1如何改变frame的大小view2都不会跟着改变。
     
     3、view2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin    |    UIViewAutoresizingFlexbleTopMargin;
     这句话的意思view2 与view1的右边坐标和底部坐标不改变。只有左边和顶部坐标随着view1的改变view2改变。
     下划线的两句都是枚举 自适应方式有很多种  其余用到后在尝试。基本要求都可以满足，苹果这点考虑的还是蛮全面的
     */
    _backView.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleRightMargin;
    
    _backView.backgroundColor=RGBA(0, 0, 0, 0.8);
    [self addSubview:_backView];
}
-(void)hideView{
    [_activity stopAnimating];
    _alertLabel.text=@"加载完成";
    
}
@end
